#include "FileSystemView.h"

FileSystemView::FileSystemView(QWidget *parent):QTreeView(parent)
{
	m_mousePressed = false;
	m_model = new QFileSystemModel(this);
	m_model->setRootPath("/home/ruben/Music/");
	setModel(m_model);
	setColumnHidden(1, true);
	setColumnHidden(2, true);
	setColumnHidden(3, true);
	setColumnHidden(4, true);
}

void FileSystemView::mousePressEvent(QMouseEvent *event){
	if(event->button() == Qt::LeftButton /*&& iconLabel->geometry().contains(event->pos())*/){
		m_mousePressed = true;
	}
	QTreeView::mousePressEvent(event);
}
#include<stdio.h>
//QString filterFileName(QString string){
	//printf("%s\n", string.toUtf8().data());
	//for(int i = string.size()-1; i >= 0; i--){
		//if(string[i] == '.'){
			//string.truncate(i+4);
			//return string;
		//}
	//}
	//return "";
//}

void FileSystemView::mouseMoveEvent(QMouseEvent */*event*/){
	if(m_mousePressed){
		m_mousePressed = false;

		QDrag *drag = new QDrag(this);
		QMimeData *mimeData = new QMimeData();

		QModelIndexList list = selectedIndexes();
		QList<QUrl> urls;
		QString data;
		for(int i = 0; i < list.size(); i++){
			data = list[i].data().toString();
			for(QModelIndex index = list[i].parent(); index.isValid(); index = index.parent())
				data.prepend(index.data().toString() + "/");
			//data = filterFileName(data);
			QFileInfo fi(data);
			if(fi.isDir()){
				QDir dir(data);
				dir.setFilter(QDir::NoDotAndDotDot);
				for(int j = 0; j < dir.count(); j++){
					urls.push_back(QUrl::fromLocalFile(dir[j]));
				}
			}else
				urls.push_back(QUrl::fromLocalFile(data));
//			printf("Creating drag for: %s\n", urls[i].toString().toUtf8().data());
		}

		for(int i = 0; i < urls.size(); i++)
			printf("Url: %s\n", urls[i].toLocalFile().toUtf8().data());

		mimeData->setUrls(urls);
		drag->setMimeData(mimeData);
		//drag->setPixmap()
		Qt::DropAction dropAction = drag->exec();
	}
}
